package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.character.GDCharTeleportList;
import org.gdstash.character.GDCharUID;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.GDAbstractRiftPane;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

public class GDCharRiftAct4Pane extends GDAbstractRiftPane {
    private JCheckBox cbAsterkarnMountains;
    private JCheckBox cbAsterkarnRoad;
    private JCheckBox cbAsterkarnValley;
    private JCheckBox cbFortIkon;
    private JCheckBox cbGatesOfNecropolis;
    private JCheckBox cbNecropolisInterior;
    private int difficulty;
    private boolean changed;

    public GDCharRiftAct4Pane(final int difficulty, final int direction) {
        this.difficulty = difficulty;
        this.changed = false;
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbAsterkarnMountains).addComponent(this.cbAsterkarnRoad).addComponent(this.cbAsterkarnValley).addComponent(this.cbFortIkon).addComponent(this.cbGatesOfNecropolis).addComponent(this.cbNecropolisInterior));
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbAsterkarnMountains)).addGroup(layout.createParallelGroup().addComponent(this.cbAsterkarnRoad)).addGroup(layout.createParallelGroup().addComponent(this.cbAsterkarnValley)).addGroup(layout.createParallelGroup().addComponent(this.cbFortIkon)).addGroup(layout.createParallelGroup().addComponent(this.cbGatesOfNecropolis)).addGroup(layout.createParallelGroup().addComponent(this.cbNecropolisInterior));
        if (direction == 0) {
            layout.setHorizontalGroup(vGroup);
            layout.setVerticalGroup(hGroup);
        } else {
            layout.setHorizontalGroup(hGroup);
            layout.setVerticalGroup(vGroup);
        }
        layout.linkSize(0, this.cbAsterkarnMountains, this.cbAsterkarnRoad);
        layout.linkSize(0, this.cbAsterkarnMountains, this.cbAsterkarnValley);
        layout.linkSize(0, this.cbAsterkarnMountains, this.cbFortIkon);
        layout.linkSize(0, this.cbAsterkarnMountains, this.cbGatesOfNecropolis);
        layout.linkSize(0, this.cbAsterkarnMountains, this.cbNecropolisInterior);
        layout.linkSize(1, this.cbAsterkarnMountains, this.cbAsterkarnRoad);
        layout.linkSize(1, this.cbAsterkarnMountains, this.cbAsterkarnValley);
        layout.linkSize(1, this.cbAsterkarnMountains, this.cbFortIkon);
        layout.linkSize(1, this.cbAsterkarnMountains, this.cbGatesOfNecropolis);
        layout.linkSize(1, this.cbAsterkarnMountains, this.cbNecropolisInterior);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCheck = UIManager.getDefaults().getFont("CheckBox.font");
        if (fntCheck == null) {
            fntCheck = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCheck = fntCheck.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ACT4"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbAsterkarnMountains == null) {
            (this.cbAsterkarnMountains = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbAsterkarnMountains.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_04_ASTERKARN_MOUNTAINS"));
        this.cbAsterkarnMountains.setFont(fntCheck);
        if (this.cbAsterkarnRoad == null) {
            (this.cbAsterkarnRoad = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbAsterkarnRoad.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_04_ASTERKARN_ROAD"));
        this.cbAsterkarnRoad.setFont(fntCheck);
        if (this.cbAsterkarnValley == null) {
            (this.cbAsterkarnValley = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbAsterkarnValley.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_04_ASTERKARN_VALLEY"));
        this.cbAsterkarnValley.setFont(fntCheck);
        if (this.cbFortIkon == null) {
            (this.cbFortIkon = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbFortIkon.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_04_FORT_IKON"));
        this.cbFortIkon.setFont(fntCheck);
        if (this.cbGatesOfNecropolis == null) {
            (this.cbGatesOfNecropolis = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbGatesOfNecropolis.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_04_GATES_OF_NECROPOLIS"));
        this.cbGatesOfNecropolis.setFont(fntCheck);
        if (this.cbNecropolisInterior == null) {
            (this.cbNecropolisInterior = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbNecropolisInterior.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_04_NECROPOLIS_INTERIOR"));
        this.cbNecropolisInterior.setFont(fntCheck);
    }

    @Override
    public boolean hasChanged() {
        return this.changed;
    }

    @Override
    public void setChanged(final boolean changed) {
        this.changed = changed;
    }

    public void setSelected(final boolean selected) {
        this.cbAsterkarnMountains.setSelected(selected);
        this.cbAsterkarnRoad.setSelected(selected);
        this.cbAsterkarnValley.setSelected(selected);
        this.cbFortIkon.setSelected(selected);
        this.cbGatesOfNecropolis.setSelected(selected);
        this.cbNecropolisInterior.setSelected(selected);
    }

    @Override
    public List<GDCharUID> getRiftList(final boolean found) {
        final List<GDCharUID> list = new LinkedList<GDCharUID>();
        if (this.cbAsterkarnMountains.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_04_ASTERKARN_MOUNTAINS);
        }
        if (this.cbAsterkarnRoad.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_04_ASTERKARN_ROAD);
        }
        if (this.cbAsterkarnValley.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_04_ASTERKARN_VALLEY);
        }
        if (this.cbFortIkon.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_04_FORT_IKON);
        }
        if (this.cbGatesOfNecropolis.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_04_GATES_OF_NECROPOLIS);
        }
        if (this.cbNecropolisInterior.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_04_NECROPOLIS_INTERIOR);
        }
        return list;
    }

    @Override
    public void setChar(final GDChar gdc) {
        this.setSelected(false);
        this.changed = false;
        if (gdc == null) {
            return;
        }
        final List<GDCharUID> list = gdc.getRiftList(this.difficulty);
        for (final GDCharUID uid : list) {
            if (uid.equals(GDCharTeleportList.UID_RIFT_04_ASTERKARN_MOUNTAINS)) {
                this.cbAsterkarnMountains.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_04_ASTERKARN_ROAD)) {
                this.cbAsterkarnRoad.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_04_ASTERKARN_VALLEY)) {
                this.cbAsterkarnValley.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_04_FORT_IKON)) {
                this.cbFortIkon.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_04_GATES_OF_NECROPOLIS)) {
                this.cbGatesOfNecropolis.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_04_NECROPOLIS_INTERIOR)) {
                this.cbNecropolisInterior.setSelected(true);
            }
        }
    }

    private class ChangeActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            GDCharRiftAct4Pane.this.changed = true;
        }
    }
}
